AddCSLuaFile("shared.lua")
AddCSLuaFile("cl_init.lua")

include("shared.lua")
include("sky_titans/shared/st_ammo.lua")

function ENT:Initialize()
	self.Entity:SetModel("models/props_junk/PopCan01a.mdl") 
	self.Entity:PhysicsInit(SOLID_VPHYSICS)
	self.Entity:SetMoveType( MOVETYPE_NONE )
	self.Entity:SetSolid( SOLID_NONE ) 
    self.Entity:SetColor( 0,0,0,255)

    local ammotable = st_ammo[self.ammotype]
    self.dmgmult = ammotable.dmgmult or 1
    self.dmgtype = ammotable.dmgtype or "pierce"
    self.dmgradius = ammotable.dmgradius or {0,0}
    self.bulletspeed = ammotable.bulletspeed or 200
    self.bulletgravity = ammotable.bulletgravity or 0.25
    self.bulletlife = ammotable.bulletlife or 1
    self.effect = ammotable.effect or {"st_explosion",1,"explode3.wav",100,100,1}
	
    self.Vel = self.Norm*self.bulletspeed
    
    self.dietime = CurTime()+self.bulletlife
	self.Entity:NextThink( CurTime() )
	
	util.SpriteTrail(self.Entity, 0, Color(200,200,200), false, 50, 1, 0.5, 1/32, "trails/smoke.vmt")
end

function ENT:Think()

self.Vel = self.Vel - Vector(0,0,self.bulletgravity)

local tr = {}
	tr.start = self.Entity:GetPos()
	tr.endpos = self.Entity:GetPos() + self.Vel
	tr.filter = {self.Entity,self.Gun}
	local trace = util.TraceLine( tr )
	self.Entity:SetPos(	trace.HitPos )
	self.Entity:SetAngles( trace.Normal:Angle()+Angle(90,0,0) )
	
	if( trace.Hit or self.dietime<CurTime() ) then
		local ShouldExplode = true
		if(trace.Entity:IsValid() and !trace.Entity:IsWorld()) then
            ent = trace.Entity
            
            if(ent.Core==self.Core) then 
                ShouldExplode = false
            else
                if(self.dmgradius[2]==0) then STDamage( self, ent, self.damage*self.dmgmult, self.dmgtype) end
            end
        end
		
        if(ShouldExplode==true) then
            STAreaDamage( self, self.damage*self.dmgmult, self.dmgtype, self.Entity:GetPos(), self.dmgradius[1], self.dmgradius[2] )
            
            local effectdata = EffectData()
            effectdata:SetOrigin( self.Entity:GetPos() )
            effectdata:SetScale( self.effect[2] )
            effectdata:SetNormal( self.Vel )
            util.Effect( self.effect[1] , effectdata )
            sound.Play(self.effect[3],trace.HitPos,self.effect[4],self.effect[5],self.effect[6])
        end
        
        self.Entity:Remove()
	end

self.Entity:NextThink( CurTime() + 0.02 )
return true
end

